package la.iok.hzsvn.lewin.movie.cloud.controller;

import la.iok.hzsvn.lewin.movie.cloud.entity.UserRole;
import la.iok.hzsvn.lewin.movie.cloud.service.UserRoleService;
import la.iok.hzsvn.lewin.movie.cloud.service.UserService;
import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.model.UserRoleDetail;
import la.iok.hzsvn.lewin.movie.core.model.UserVo;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("userRoles")
public class UserRoleController {
    private final UserRoleService userRoleService;
    private final UserService userService;

    public UserRoleController(UserRoleService userRoleService,
                              UserService userService) {
        this.userRoleService = userRoleService;
        this.userService = userService;
    }

    @GetMapping("user/{userId}")
    public ListDataResponse<UserRoleDetail> list(@PathVariable Long userId){
        return new ListDataResponse<>(userRoleService.details(userId));
    }

    @GetMapping("select/{roleId}")
    public SingleDataResponse<PageVo<UserVo>> select(@RequestHeader Token token, @PathVariable Long roleId){
        return new SingleDataResponse<>(userRoleService.selectOrgUnitroles(token,token.getOrgUnitId(),roleId));
    }



    @DeleteMapping({"{id}"})
    public OperatorResponse delete(@RequestHeader Token token,@PathVariable Long id){
        userService.remove(token,id);
        return OperatorResponses.success();
    }

    @PutMapping({"/score/increase/{userId}/{score}"})
    public OperatorResponse increaseScore(@RequestHeader Token token,@PathVariable Long userId,@PathVariable Long score){
        token.validAnyAuthority(RoleEnum.CarpoolMaster);
        userRoleService.increaseScore(userId,token.getOrgUnitId(),score);
        return OperatorResponses.success();
    }

    @PutMapping({"/score/reduceScore/{userId}/{score}"})
    public OperatorResponse reduceScore(@RequestHeader Token token,@PathVariable Long userId,@PathVariable Long score){
        token.validAnyAuthority(RoleEnum.CarpoolMaster);
        userRoleService.reduceScore(userId,token.getOrgUnitId(),score);
        return OperatorResponses.success();
    }

    @GetMapping("select/score/{userId}")
    public SingleDataResponse<List<UserRole>> selectScore(@RequestHeader Token token, @PathVariable Long userId) {
        return new SingleDataResponse<>(userRoleService.selectScore(userId, token.getOrgUnitId()));
    }





}
